package com.c2dm.server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Scanner;

public class getToken {
	
	String token()
	{
		String rValue="";
		String data = "accountType=HOSTED_OR_GOOGLE"+
		//String data = "accountType=HOSTED"+
		"&Email="+"hung.pochin@gmail.com"+
		"&Passwd="+"bojin0526"+
		"&service="+"ac2dm"+
		"&source=iac-stockprice2u-11";  

	   try
       {
		  StringBuffer sb = new StringBuffer(); 
          URL url=new URL("https://www.google.com/accounts/ClientLogin");
          URLConnection conn = url.openConnection();
          conn.setDoOutput(true); 
          OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
          wr.write(data);
          wr.flush();
          
          BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
          String line;
           while ((line = rd.readLine()) != null) {
        	// Process line...
        	   sb.append(line); 
        	}
           // rValue=sb.toString();
            Scanner sc = new Scanner(sb.toString()).useDelimiter("=");
            
            while(sc.hasNext()){
             String tmp = sc.next()/*.replaceAll("\n\r", "")*/.replace("\n", "");
             if(tmp.trim().endsWith("Auth")){
            	 rValue = sc.next()/*.replaceAll("\n\r", "")*/.replace("\n", "");
             }    
            }
        	wr.close();
        	rd.close(); 
        	return rValue;

        }
        catch(MalformedURLException e)
        {
        }
        catch(IOException e)
        {
        }
        return rValue;
	}

}
